import { createWebHistory, createRouter } from 'vue-router'

// import HomeView from './HomeView.vue'
// import AboutView from './AboutView.vue'

import IndexView from '@/views/index/index.vue'
import ClazzView from '@/views/clazz/index.vue'
import StuView from '@/views/stu/index.vue'
import DeptView from '@/views/dept/index.vue'
import EmpView from '@/views/emp/index.vue'
import EmpReportView from '@/views/report/emp/index.vue'
import StuReportView from '@/views/report/stu/index.vue'
import LoginView from '@/views/login/index.vue'
import Layout from '@/views/layout/index.vue'

const routes = [
    {
      path: '/',
      name: '/',
      redirect: '/index',
      component: Layout,
      children: [
          { name:"index", path: '/index', component: IndexView },
          { name:"clazz", path: '/clazz', component: ClazzView },
          { name:"stu", path: '/stu', component: StuView },
          { name:"dept", path: '/dept', component: DeptView },
          { name:"emp", path: '/emp', component: EmpView },
          { name:"empReport", path: '/empReport', component: EmpReportView },
          { name:"stuReport", path: '/stuReport', component: StuReportView },
      ]
    },
    { name: "login", path: '/login', component: LoginView }
]

const router = createRouter({
  history: createWebHistory(),
  routes,
})

// 全局前置守卫--监控是否登录
router.beforeEach((to, from, next) => {
  console.log('全局前置守卫', to, from)
  // 如果是登录页面，直接放行
  if (to.path === '/login') {
    return next()
  }
  // 获取token
  const token = localStorage.getItem('loginUser')
  // 如果有token，直接放行
  if (token) {
    return next()
  }
  // 如果没有token，跳转到登录页面
  next('/login')
})

export default router